#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "user.h"

#define NUMBERBUFSIZE 10
#define NAMEBUFSIZE 50
#define PASSWORDBUFSIZE 101

static void map_user_info(user* new_user, char* number, char* name, int total, int rest, char* password)
{
    new_user->number = strdup(number);
    new_user->name = strdup(name);
    new_user->total = total;
    new_user->rest = rest;
    new_user->password = strdup(password);
}

user* create_users_list(char* users_db_path)
{
    FILE* users_db = initialize_file(users_db_path);
    if(users_db == NULL)
        return NULL;
    user* first_user = NULL;
    user* current_user = NULL;
    char number_buf[NUMBERBUFSIZE];
    char name_buf[NAMEBUFSIZE];
    char password_buf[PASSWORDBUFSIZE];
    int total_buf = 0;
    int rest_buf = 0;
    while(feof(users_db) == 0)
    {
        if(fscanf(users_db, "%[0-9]$%[^$]$%d$%d$%s\n", &number_buf, &name_buf, &total_buf, &rest_buf, &password_buf) == 5)
        {
            if(first_user == NULL)
            {
                first_user = (user*)malloc(sizeof(user));
                first_user->previous = NULL;
                first_user->next = NULL;
                map_user_info(first_user, number_buf, name_buf, total_buf, rest_buf, password_buf);
                current_user = first_user;
            }
            else
            {
                user* temp_user = (user*)malloc(sizeof(user));
                temp_user->previous = current_user;
                temp_user->next = NULL;
                map_user_info(temp_user, number_buf, name_buf, total_buf, rest_buf, password_buf);
                current_user->next = temp_user;
                current_user = temp_user;
            }
        }
        else
        {
            break;
        }
    }
    fclose(users_db);
    return first_user;
}

void free_user(user* target)
{
    free(target->number);
    free(target->name);
    free(target->password);
}

void show_user(user* users_list)
{
    if(users_list != NULL)
    {
        do
        {
            printf("--------------------\n编号：%s\n姓名：%s\n可借总数：%d\n可借剩余：%d\n--------------------\n", users_list->number, users_list->name, users_list->total, users_list->rest);
        }
        while((users_list = users_list->next) != NULL);
    }
}

user* create_user()
{
    user* new_user = (user*)malloc(sizeof(user));
    char number_buf[NUMBERBUFSIZE];
    char name_buf[NAMEBUFSIZE];
    int total_buf = 0;
    char password_buf[PASSWORDBUFSIZE];
    printf("编号：");
    scanf("%s", &number_buf);
    fflush(stdin);
    if(strlen(number_buf) != 9)
    {
        printf("编号应为 9 位\n");
        free(new_user);
        return NULL;
    }
    printf("姓名：");
    scanf("%s", &name_buf);
    fflush(stdin);
    printf("可借总数：");
    scanf("%d", &total_buf);
    fflush(stdin);
    while(total_buf < 0)
    {
        printf("数量应为正数\n");
        printf("可借总数：");
        scanf("%d", &total_buf);
        fflush(stdin);
    }
    printf("请输入密码：");
    scanf("%s", &password_buf);
    fflush(stdin);
    new_user->previous =NULL;
    new_user->next = NULL;
    map_user_info(new_user, number_buf, name_buf, total_buf, total_buf, password_buf);
    return new_user;
}

user* search_user(user* users_list, char* number)
{
    if(users_list != NULL)
    {
        user* result = NULL;
        do
        {
            if(strcmp(users_list->number, number) == 0)
            {
                result = users_list;
                break;
            }
        }
        while((users_list = users_list->next) != NULL);
        return result;
    }
    else
        return NULL;
}

int user_borrow_book(user* users_list, char* user_number)
{
    user* target_user = search_user(users_list, user_number);
    if(target_user != NULL)
    {
        if(target_user->rest > 0)
            target_user->rest -= 1;
        else
            return 0;
    }
    else
        return 0;
    return 1;
}

int user_return_book(user* users_list, char* user_number)
{
    user* target_user = search_user(users_list, user_number);
    if(target_user != NULL)
    {
        if(target_user->rest < target_user->total)
            target_user->rest += 1;
        else
            return 0;
    }
    else
        return 0;
    return 1;
}
